package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpHRSkillMaintenancePageUtil extends SeleniumPageUtil {

	public final static By SMM_SPAN_MAIN_HEADER = By.id("ctl44_hdrSkillManagement_lblHeader");
	public final static By SMM_LINK_MAINTENANCE = By.xpath("//*[@id='1672c015-9739-48d5-a626-62e2d1b5835c']/a");
	
	public final static By SMM_SPAN_MAINTENANCE_HEADER = By.id("ctl44_SkillMaintenanceReport_Header_lblHeader");
	
	public final static By SMM_IMG_REPORT_PREFERENCES = By.id("ctl44_SkillMaintenanceReport_Header_imgPreferences");
	public final static By SMM_IMG_EXPORT = By.id("ctl44_SkillMaintenanceReport_Header_imgExcelExport");
	public final static By SMM_IMG_REFRESH = By.id("ctl44_SkillMaintenanceReport_Header_imgRefresh");
	
	public final static By SMM_LINK_NEW = By.xpath("//*[@id='625f8471-6652-4a58-a796-b12fcc87d973']/a");
	
	public final static By SMM_TD_PAGE = By.xpath("//*[@id='dvSkillMaintenanceReport']/table[2]/tbody/tr/td[1]");
	public final static By SMM_SPAN_PAGE_START_NO = By.id("ctl44_SkillMaintenanceReport_PagingTop_lblPagingPageNumber");
	public final static By SMM_TD_OF = By.xpath("//*[@id='dvSkillMaintenanceReport']/table[2]/tbody/tr/td[3]");
	public final static By SMM_SPAN_PAGE_COUNT = By.id("ctl44_SkillMaintenanceReport_PagingTop_lblPagingPageCount");
	
	public final static By SMM_INPUT_FIRST = By.id("ctl44_SkillMaintenanceReport_PagingTop_imgFirst");
	public final static By SMM_INPUT_PREVIOUS = By.id("ctl44_SkillMaintenanceReport_PagingTop_imgPrevious");
	public final static By SMM_INPUT_NEXT = By.id("ctl44_SkillMaintenanceReport_PagingTop_imgNext");
	public final static By SMM_INPUT_LAST = By.id("ctl44_SkillMaintenanceReport_PagingTop_imgLast");
	
	public final static By SMM_IMG_FILTER_1 = By.xpath("//*[@id='ctl44_SkillMaintenanceReport_ReportOutput']/table/tbody/tr[1]/th[1]/img");
	public final static By SMM_IMG_FILTER_2 = By.xpath("//*[@id='ctl44_SkillMaintenanceReport_ReportOutput']/table/tbody/tr[1]/th[2]/img");
	public final static By SMM_IMG_FILTER_3 = By.xpath("//*[@id='ctl44_SkillMaintenanceReport_ReportOutput']/table/tbody/tr[1]/th[3]/img");
	public final static By SMM_FILTER_INPUT = By.id("ctl44_SkillMaintenanceReport_txtFilterInput");
	public final static By SMM_FILTER_BTN_ENTER = By.id("ctl44_SkillMaintenanceReport_btnFilterEnter");
	public final static By SMM_FILTER_BTN_CANCEL = By.id("ctl44_SkillMaintenanceReport_btnFilterCancel");
	
	public final static By SMM_TH_CATEGORY = By.xpath("//*[@id='ctl44_SkillMaintenanceReport_ReportOutput']/table/tbody/tr[2]/th[1]");
	public final static By SMM_TH_SUB_CATEGORY = By.xpath("//*[@id='ctl44_SkillMaintenanceReport_ReportOutput']/table/tbody/tr[2]/th[2]");
	public final static By SMM_TH_SKILL = By.xpath("//*[@id='ctl44_SkillMaintenanceReport_ReportOutput']/table/tbody/tr[2]/th[3]");
	public final static By SMM_SPAN_RESULT = By.id("ctl44_SkillMaintenanceReport_ReportOutput");
	public static void enterHRSkillMaintenancePage() {
		SpHomePageUtil.enter();
		SpHomePageUtil.enterHRSkillManagementPage();
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SMM_LINK_MAINTENANCE));
		waitForResultRefresh();
	}
	public static void enterHRSkillMaintenance() {
		SpHomePageUtil.enterHRSkillManagementPage();
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SMM_LINK_MAINTENANCE));
		SeleniumUtil.waitForFixedMilliseconds(3000);
		waitForPageToLoad();
	}
	
	public static void waitForPageToLoad() {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				SMM_IMG_EXPORT, SMM_INPUT_FIRST, SMM_INPUT_PREVIOUS, 
				SMM_INPUT_NEXT, SMM_INPUT_LAST, SMM_IMG_FILTER_1, SMM_IMG_FILTER_2, SMM_IMG_FILTER_2
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_IMG_EXPORT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_IMG_FILTER_1));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_IMG_FILTER_2));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_IMG_FILTER_3));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_INPUT_FIRST));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_INPUT_PREVIOUS));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_INPUT_NEXT));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SMM_INPUT_LAST));
	}
	
	/**wait for result refresh, if time out 30 seconds not response return an exception*/
	public static void waitForResultRefresh(){
		long beforeWait = System.currentTimeMillis();
		long afterWait;
		boolean refreshEnd = SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SMM_IMG_REFRESH));
		while(refreshEnd){
			afterWait = System.currentTimeMillis();
			if((afterWait - beforeWait) < 15 * 1000){
				waitForPageToLoad();
				refreshEnd = SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(SMM_IMG_REFRESH));
			}else{
				SeleniumUtil.fail("timeout 30 seconds not response");
				return;
			}
		}
	}
}
